package algorithm.middle;

import java.util.PriorityQueue;

public class magicTower {

    public int magicTower(int[] nums) {
        int cal = 1;
        for (int num : nums) {
            cal += num;
        }
        if (cal <= 0) {
            return -1;
        }
        //保证必有解
        PriorityQueue<Integer> pq = new PriorityQueue<>();
        long blood = 1;
        int res = 0;
        for (int num : nums) {
            if (num < 0) {
                pq.offer(num);
            }
            blood = blood + num;
            if (blood <= 0) {
                //说明这时不够，需要弹出一个之前最大的用
                blood = blood - pq.poll();
                res++;
            }
        }
        return res;
    }
}



